
/*--------------------快捷菜单拖拉初始化方法   -------------------*/
//容器
function $(obj){return document.getElementById(obj)}	
var ctlArray=["cut_body0","cut_body1"];


//定义各版块参数(cutId是主键用于修改哪一条记录的排序id):
//onclick='changeStyle(this);保存当前菜单的ID
//定义并获取版块模板
	var rssTemplate="<div class='cut_divdrag_body' id='menu_{$cutId$}'>";
	rssTemplate+="<div class='dragtitle' id='menu_{$cutId$}_h' onclick='changeStyle(this);'>";
	rssTemplate+="<table border='0' cellpadding='2' cellspacing='0' width='90%'>";
	rssTemplate+= " <tr onclick='clickListTr_p(this);' onmouseout='outListTr_p(this)' height='25px'> <td class='dragTableHeader'";
	rssTemplate+= "><img  src='{$imgpath$}' align='absmiddle'/>{$shorcutMenuName$}";



//拖拉对象初始化
function init_Drag(){
   
	var elementObj= [];

	var el=document.getElementById(ctlArray[0]);

	var el1=document.getElementById(ctlArray[1]);

	elementObj[0]=el;

	elementObj[1]=el1;

	_initScrollDrag(elementObj,"cut_divdrag_body","_h");


}

window.onload=function(){init_Drag();}




//获取带数据的模板列表
function getRssTemp(cutMenuArray){

    var rssTemp="";
	var menuTemp = rssTemplate;
	for(var i=0;i<cutMenuArray.length;i++){
		
		menuTemp = menuTemp.replace(/{\$imgpath\$}/g,cutMenuArray[i][2]);	
		
		menuTemp = menuTemp.replace(/{\$shorcutMenuName\$}/g,cutMenuArray[i][3]);
		
		menuTemp = menuTemp.replace(/{\$cutId\$}/g,cutMenuArray[i][4]);
		
		if(cutMenuArray[i][1]=="select"){
		   //var selectTemp = "<td  align=\"left\" class=\"personal_out_list\">";
		   var selectTemp = "<span style=\"cursor:hand\" onclick=\"showCutDiv('menuSelectDiv',\'"+cutMenuArray[i][0]+"',this);\"><img align=\"absmiddle\" src=\"images/ico_menu_down.gif\"></span>";
		   menuTemp +=selectTemp;
		   //alert(menuTemp);
		}
		
		//clipboardData.setData("text", menuTemp);copy文本
		rssTemp += menuTemp+" </td></tr> </table></div></div>";
		
		menuTemp = rssTemplate;
	}
	
	return rssTemp;
	

}





/*---------------   改变tr样式    ----------------*/
var gListOver = null;
function outListTr_p(obj){
	if(obj != gListOver){
		obj.className = "personal_out_list";
	}
}
function clickListTr_p(obj){
	if(gListOver == null){
		obj.className = "personal_click_list";
	} else {
		if (gListOver != obj) {
			gListOver.className = "personal_out_list";
			obj.className = "personal_click_list";
		}
	}
	gListOver = obj;
}



	


/*-------------------创建一个唯一的div同时异步读取快捷菜单中所属的所有的快捷方式----------------*/

//当前newdiv
var newSelectDivObj = document.getElementById("menuSelectDiv");	
function showCutDiv(newDivId,cutType,imgObj){
    
	if(newSelectDivObj==null){
		newSelectDivObj = createCutDiv(newDivId);
	}
	resetDivPosition(newSelectDivObj,imgObj.parentNode);
	
	//getShortcutList("indexAction.do?method=getAllCutModes&cut_type="+cutType);
	var actionUrl = "indexAction.do?method=getAllCutModes&cut_type="+cutType;
	startRequest("POST",actionUrl,null,generateDivDatas);//ajaxtool.js里的方法
	
}
 

//创建快捷方式所在的div
function createCutDiv(newDivId){
	  var newDivEle  =  document.createElement("div");
	  newDivEle.id = newDivId;
	  newDivEle.style.position = "absolute";
	  newDivEle.style.zIndex =9999; 
	  newDivEle.attachEvent("onblur",function(){hideDivNew();});
	  document.body.appendChild(newDivEle);
	  return newDivEle;
}



 //定位当前点击对象
	function getOffset(el){
	   var x = 0;
	   var y = 0;
	  
	   while(el){
		 x+= el.offsetLeft;

		 y+= el.offsetTop;  

		 el = el.offsetParent;
		 	
	   
	   }

	   return [x,y];
	
	}
	

	

	//隐藏div
	function hideDivNew(){
		newSelectDivObj.style.display="none";
	}
	
	//显示div
	function showDivNew(){
	   newSelectDivObj.style.visibility="visible";
	   newSelectDivObj.style.display = "block";
	   newSelectDivObj.focus();
   }







//重新定位div
function resetDivPosition(newDivEle,tdObj){

  var clickEle =event.srcElement;
  
  var clickObjHeight = clickEle.clientHeight==undefined?0:clickEle.clientHeight;
  
  newDivEle.style.top = Util.getOffset(clickEle)[1]  + clickObjHeight +"px";
  
  //newDivEle.style.left = getOffset(clickEle)[0] - tdObj.offsetWidth + 30+"px";  
  newDivEle.style.left = Util.getOffset(clickEle)[0];
  
  	//newDivEle.style.top=window.event.y + 5;
	//newDivEle.style.left=window.event.x + 10;
  
}
	
 
 
 //根据返回的数据进行div构造
 function generateDivDatas(){
 
 		
 		
 		var xmlData = http_51ajax.responseXml;
		var rootEle = xmlData.documentElement;
		
		var dataList = new Array();
		
		if (rootEle) dataList = rootEle.getElementsByTagName('shortcut');
		else return ;
		
		var dataLen = dataList.length;
		var selectDivContent="";
     	for(var i=0;i<dataLen;i++){
			 var cutMode = dataList[i].childNodes[0].firstChild.nodeValue;
			  
			 var cutId = dataList[i].childNodes[1].firstChild.nodeValue;
			
			 var userId = dataList[i].childNodes[2].firstChild.nodeValue;
			 var isCheck = userId=="null"?"":"checked";
			 var checkboxHTML = "<tr onfocus=\"showDivNew();\"><td><input type=\"checkbox\" name=\"checkbox\" "+isCheck+" id=\"checkbox_"+i+"\" value=\""+cutId+"\" onclick=\"javascript:checkboxChange(this)\" onfocus=\"showDivNew();\">";
			 selectDivContent += checkboxHTML+"&nbsp;&nbsp;&nbsp;<label>"+cutMode+"</label> </td></tr>"; 
	    }
	     var divFirstInnerHtml = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	     var divLastInnerHtml = "</table>";
		 newSelectDivObj.innerHTML = divFirstInnerHtml+selectDivContent+divLastInnerHtml;
		showDivNew();
		
 
 }
 
 
 //dojo查询数据，返回当前快捷菜单所有的快捷方式
 function getShortcutList(url){
		dojo.io.bind({
				url:url,
				handle: function(type, data, evt){ 
    			    if(type == "load"){
    			       //alert("data");
            			generateDivDatas(data);
		        	 }else if(type == "error"){ 
						postMsg(data,"error")
			         }else{ 
						postMsg(data,"error")        	
			   	     }
	    		 },
				mimetype:"text/xml",
				preventCache: false,
				useCache: false
			})	
			 
	}
 
 
  //取消或选中div中的checkbox时ajax异步更新快捷方式
  function checkboxChange(currentbox){
  
  	var optionFlag = "add";
  	if(!currentbox.checked){
  	  
  	  optionFlag = "del";
  	
  	}
  	
    var actionUrl="shortcut.do?method=updateShortcut&optionFlag="+optionFlag+"&shortcutId="+currentbox.value;
   // saveDragSource(actionUrl,"shortcut");//此方法要调用显示快捷菜单的接口
    
    startRequest("POST",actionUrl,null,null);//ajaxtool.js里的方法
  
  }
 
 
 
 


/*-----------------------上下左右移动快捷菜单 ----------------------*/
var dragdivId = null;
 var containerFlag = ctlArray[0];

 function changeStyle(divObj){
    dragdivId = divObj.id; 
	  
 }


//增加或删除快捷菜单
function moveCutMode(flag){
    var containerBody="";
    var applyId = "";
	if(dragdivId!=null)
		containerFlag = $(dragdivId).parentNode.parentNode.id;

    if(flag=="add"){
        if(dragdivId==null||containerFlag!=ctlArray[0]){
			alert("请选择可用快捷菜单!");
			return false;
		}
    	containerBody = ctlArray[1];
    	applyId = 1;
		containerFlag = ctlArray[1];
    	
    }
       
    else{
        if(dragdivId==null||containerFlag!=ctlArray[1]){
			alert("请选择当前快捷菜单!");
			return false;
		}
    	containerBody = ctlArray[0];
    	applyId = 0;
		containerFlag = ctlArray[0];
    	
    }
       
	//$(containerBody).insertAdjacentElement('beforeEnd',$(dragdivId).parentNode);
		var insertObj = $(containerBody).insertBefore($(dragdivId).parentNode,$(containerBody).lastChild);
	    var allId = dragdivId.split("_");
		var sortId = insertObj.parentNode.childNodes.length-2;
		var cutId = allId[1];
		var insertParentChilds = insertObj.parentNode.childNodes;
	    var cutUrl = "shortcut.do?method=updateCutMenuWhenMove&applyId="+applyId+"&cutId="+cutId+"&sortId="+sortId;
	    //saveDragSource(cutUrl,"menu");//toolbar_drag.js里的dojo实现方法
	    startRequest("POST",cutUrl,null,showParentMenuDiv);//ajaxtool.js里的方法

}

//ajax回调方法
function showParentMenuDiv(){ 		
	//if(http_51ajax.responseText=="success"){
		
		 var doc = http_51ajax.responseXML;
			
		 var toolBarList = new Array();
		 if(Browser.isIE){
			try{
				var root = doc.childNodes; //根节点
				
				var toolBarNum = root[1].childNodes.length; //一级节点数<tool>
				for(i = 0; i < toolBarNum; i++){
					toolBarList[i] = new Array();
					var rootChildNode = root[1].childNodes[i];
					var toolBarPropertiesNum = rootChildNode.childNodes.length; //子节点数
					for(j = 0; j < toolBarPropertiesNum; j++){
						var toolbarNode=rootChildNode.childNodes[j];
						toolBarList[i][j] = toolbarNode.childNodes[0].nodeValue;//子节点值
					}
				}
			}catch (e){}
		}else{
			
			try{
				var root = doc.childNodes[0]; //根节点<tool>
				var toolBarNum = root.childNodes.length; //一级节点数<tool>
				var n = 0;
				
				for(i = 1; i < toolBarNum; i+=2){
					toolBarList[n] = new Array();
					var rootChildNode = root.childNodes[i];
					var toolBarPropertiesNum = rootChildNode.childNodes.length; //子节点数
					var m = 0;
					for(j = 1; j < toolBarPropertiesNum; j+=2){
						var toolbarNode=rootChildNode.childNodes[j];
						toolBarList[n][m] = toolbarNode.textContent;//子节点值
						m++;	
					}
					n++;
				}
			}catch(e){}
			
		}
		var htmlStr = "";
		for(i = 0; i < toolBarList.length; i++){ 
			htmlStr += "<td><a " + toolBarList[i][5] + "><img src='" + toolBarList[i][4] + "' alt='" + toolBarList[i][2] + "' border='0' /></a></td>";
		}
		//var toolbar = createToolBar("shortcut.do?method=generateToolBarXml&time" + new Date().getTime());		
    	window.parent.parent.document.getElementById("shortcutMenuDiv").innerHTML= htmlStr;
    	
    	
	//}
}

//上下移动当前快捷菜单
function upDownCutmode(flag){
	if(dragdivId!=null)
		containerFlag = $(dragdivId).parentNode.parentNode.id;

	if(dragdivId==null||containerFlag!=ctlArray[1]){
		alert("请选择当前快捷菜单!");
		return false;
	}
	
	var movedObj=null;
	var containerBody = ctlArray[1];
	var allId = dragdivId.split("_");
	var sortId;
	var cutId = allId[1];
	
	var parentNode = $(dragdivId).parentNode.parentNode;
	
	for(var i=0;i<parentNode.childNodes.length;i++){
	    var node = parentNode.childNodes(i);
		if(node.id == $(dragdivId).parentNode.id){
		   if(flag=="up"){
		   		if(i>1){
		   		    movedObj = parentNode.childNodes(i-1);
		   		    sortId = i-1;
		   		
		   		}
		   			
		   
		   }
		   
		   if(flag=="down"){
		       if(i<parentNode.childNodes.length-2){
		       		movedObj = parentNode.childNodes(i+1);
		       		sortId = i+1;
		       }
		         
		   }
		   
		   break;
		   
		}
			
	
	}
	
	if(movedObj==null){
		return;
	
	}
	
	//交换两个层的位置
	$(dragdivId).parentNode.swapNode(movedObj);
	
	var cutUrl = "shortcut.do?method=updateCutMenuWhenMove&applyId=1&cutId="+cutId+"&sortId="+sortId;
	 
	//saveDragSource(cutUrl,"menu");//toolbar_drag.js里的dojo实现方法
	startRequest("POST",cutUrl,null,showParentMenuDiv);//ajaxtool.js里的方法
 }
